package com.controller;

import com.po.Coach;
import com.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by 阳幸子 on 2018/5/8.
 */
@Controller
public class CoachController {

    @Autowired
    private CoachService coachService;


    @RequestMapping("/trainer_manager")
    public String trainer_manager(Model model,int page){
        int count=coachService.count();
        int pageSize=0;
        if(count%6==0){
            pageSize=count/6;
        }else{
            pageSize=count/6+1;
        }
        if(page>0){
            page=(page-1)*6;
        }

        List<Coach> listCoach=coachService.selectCoachByPage(page);
        model.addAttribute("count",count);
        model.addAttribute("listCoach",listCoach);
        model.addAttribute("pageSize",pageSize);
        return "/manager/trainer_manager";
    }
    @RequestMapping("/add_coach")
    public String add_coach(){
        return "/manager/add_trainer";
    }
    @RequestMapping("/add_coachForm")
    public String add_coachForm(Coach coach){
        coach.setUsername(coach.getNum()+"");
        coach.setPassword("111111");
        System.out.println(coach);
        coachService.addCoach(coach);
        return "redirect:/trainer_manager?page=1";
    }

    @RequestMapping("/edit_coach")
    public String edit_coach(long id,Model model){
        Coach coach=coachService.selectById(id);
        model.addAttribute("coach",coach);
        System.out.println(coach+"...");
        return "/manager/edit_trainer";
    }

    @RequestMapping(value = "/edit_coachForm",method = RequestMethod.POST)
    public String edit_coachForm(Coach coach,@RequestParam(value = "id") long id){
        System.out.println(id);
        coach.setId(id);

        System.out.println(coach);
        coachService.updateCoach(coach);
        return "redirect:/trainer_manager?page=1";
    }

    @RequestMapping("/trainer_index")
    public String trainer_index(){
        return "/trainer/trainer_index";
    }
}
